package vip.finance.makemoney.config.pinyin;

import com.github.promeg.pinyinhelper.Pinyin;
import com.github.promeg.pinyinhelper.PinyinMapDict;
import com.github.promeg.tinypinyin.lexicons.java.cncity.CnCityDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义拼音翻译
 *
 * @author 杨滨
 * @date 2023年02月22日
 */
@Slf4j
@Component
@Order(1) // 默认是最低优先级,值越小优先级越高
public class PinYinConfig implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 添加中文城市词典
        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance()));
        // 添加自定义词典
        Pinyin.init(Pinyin.newConfig()
                .with(new PinyinMapDict() {
                    @Override
                    public Map<String, String[]> mapping() {
                        HashMap<String, String[]> map = new HashMap<String, String[]>();
                        map.put("重庆", new String[]{"CHONG", "QING"});
                        map.put("银行", new String[]{"YING", "HANG"});
                        return map;
                    }
                }));
        log.info("初始化自定义拼音词典成功");
    }
}
